package fucai.com.fucai.ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.RadioGroup;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import fucai.com.fucai.R;
import fucai.com.fucai.blmanager.DataManager;
import fucai.com.fucai.blmanager.NetDataManger;
import fucai.com.fucai.db.DbHepler;
import fucai.com.fucai.db.FuCaiNumber;
import fucai.com.fucai.fragment.CountingFragment;
import fucai.com.fucai.fragment.ForecastFragment;
import fucai.com.fucai.fragment.MeFragment;
import fucai.com.fucai.fragment.ToolsFragment;
import fucai.com.fucai.initData.InitDataDB;
import fucai.com.fucai.utils.FragmentTabUtils;

public class NiuFuCai extends BaseActivity {
    public static final int UPDATE_IS_OK = 1003;
    public static final int UPDATE_IS_ERROR = 1004;
    public static final int UPDATE_IS_NODATA = 1005;
    public static final int UPDATE_IS_NOT_NET = 1006;
    public static final int UPDATE_IS_OK_NET = 100;
    public static final int UPDATE_IS_ERROR_NET = 101;
    public static final int UPDATE_IS_NODATA_NET = 102;
    public static final int UPDATE_IS_NOT_NET_NET = 103;
    public static final int COPY_IS_OK_TXT = 1001;
    public static final int COPY_IS_FAILE_TXT = 1002;
    public static final int COPY_IS_FAILE_LIST = 1007;
    public static final int COPY_IS_OK_LIST = 1008;
    private List<Fragment> fragments;
    @ViewInject(R.id.main_rgs)
    private RadioGroup rgps;
    private NetDataManger mNetDataManger = null;
    private DataManager mDataManger = null;
    private MyHandler mHandler = null;
    private MyUpdateHandler myUpdateHandler = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_niu_fu_cai);
        initView();
    }

    private void initView() {
        mDataManger = new DataManager(this);
        mNetDataManger = new NetDataManger(this);
        mHandler = new MyHandler(mDialog);
        myUpdateHandler = new MyUpdateHandler(mDialog);
        ViewUtils.inject(this);
        //copyDataBaseToPhone();
        initData();
        update();
        fragments = new ArrayList<>();
        fragments.add(ForecastFragment.getInstance());
        fragments.add(CountingFragment.getInstance());
        fragments.add(ToolsFragment.getInstance());
        fragments.add(MeFragment.getInstance());
        FragmentTabUtils utils = new FragmentTabUtils(getSupportFragmentManager(), fragments, R.id.fragment_main_container, rgps);
    }

    public void initData() {
        //第一次复制数据
        if (!mDataManger.checkIsFrist()) {
            mDialog.show();
            mDialog.setMessage("初始化数据...");
            mDataManger.copyTxtToDb(mHandler, this);
        } else {
            return;
        }
    }


    /**
     * 更新数据
     */
    public void update() {
       /* if (mNetDataManger.checkNetwork(NiuFuCai.this)) {*/
        mNetDataManger.getNewDataForService(myUpdateHandler);
       /* } else {
            mNetDataManger.showToastMessage("没有网络", 1000);
        }*/
    }


    class MyHandler extends Handler {
        ProgressDialog dialog;

        public MyHandler(ProgressDialog dia) {
            this.dialog = dia;
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            dialog.dismiss();
            switch (msg.what) {
                case COPY_IS_OK_TXT:
                    mDataManger.copyDataToDb(mHandler, getApplication());
                    break;
                case COPY_IS_FAILE_TXT:
                    break;
                case COPY_IS_OK_LIST:
                    //更新成功 更改第一次 下次不在更新直接从网络去
                    String[] info = new String[]{getResources().getString(R.string.istrue)};
                    mDataManger.saveUserLoginBasicInfo(info);
                    DbHepler.getInstance().closeDb();
                    break;
                case COPY_IS_FAILE_LIST:
                    break;
                default:
                    break;
            }
        }
    }

    class MyUpdateHandler extends Handler {
        ProgressDialog dia;

        public MyUpdateHandler(ProgressDialog dia) {
            this.dia = dia;
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case UPDATE_IS_OK:
                    final int updataNumber = mNetDataManger.isUpdate(msg.obj.toString());
                    if (updataNumber == 0) {
                        return;
                    } else {
                        //网络数据下载
                        new AlertDialog.Builder(NiuFuCai.this)
                                .setMessage("更新新数据，预测更准确")
                                .setTitle("数据有更新了")
                                .setNegativeButton("取消", null)
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dia.show();
                                        dia.setMessage("更新数据中...");
                                        mNetDataManger.getFromService(myUpdateHandler, updataNumber);
                                    }
                                }).show();
                    }
                    break;
                case UPDATE_IS_NOT_NET:
                    break;
                case UPDATE_IS_ERROR:
                    break;
                case UPDATE_IS_NODATA:
                    break;
                case UPDATE_IS_OK_NET:
                    //网络下载更新的数据成功 开始更新
                    mNetDataManger.addNetListToDb(getApplication(), myUpdateHandler);
                    dia.dismiss();
                    mNetDataManger.showToastMessage("数据更新完毕", 1000);
                    DbHepler.getInstance().closeDb();
                    break;
                case UPDATE_IS_NOT_NET_NET:
                    break;
                case UPDATE_IS_ERROR_NET:
                    break;
                case UPDATE_IS_NODATA_NET:
                    break;

                default:
                    break;
            }
        }
    }

}
